"use strict";
cc._RF.push(module, '902e8+pVPJKuK/Usfi5DRbk', 'robot_common');
// scripts/robotSys/robot_common.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.roomInfo = exports.playerInfo = void 0;
var playerInfo = /** @class */ (function () {
    function playerInfo() {
        this.uid = null;
        this.name = null;
        this.value = null;
        this.prepared = 0;
        this.grabRes = -1;
        this.handCards = [];
        this.outCards = [];
    }
    return playerInfo;
}());
exports.playerInfo = playerInfo;
var _ = require("lodash");
var pukeData_1 = require("../pukeData");
var roomInfo = /** @class */ (function () {
    function roomInfo() {
        this.players = {};
        this.timeto = null;
        this.dzid = null;
        this.lastOut = null; // {outStyle, uid}
    }
    roomInfo.prototype.dealTask = function (json) {
        switch (json.task) {
            case 'joinPlayer': return this.joinPlayer(json.data);
            case 'preparePlayer': return this.preparePlayer(json.data);
            case 'grabCard': return this.grabCard(json.data);
            case 'playOut': return this.playOut(json.data);
            case 'quit': return this.quit(json.data);
        }
    };
    roomInfo.prototype.joinPlayer = function (data) {
        var _this = this;
        var uid = data.uid;
        this.players[uid] = new playerInfo();
        return new Promise(function (res, rej) {
            res({ event: 'joinPlayer', roomInfo: _this });
        });
    };
    roomInfo.prototype.preparePlayer = function (data) {
        var _this = this;
        var uid = data.uid;
        this.players[uid].prepared = true;
        var prepareNum = 0;
        Object.keys(this.players).map(function (uid) {
            if (_this.players[uid].prepared)
                prepareNum += 1;
        });
        if (prepareNum == 3) {
            var shuffled_cards = pukeData_1.ShuffleCards();
            var cnt = 0;
            Object.keys(this.players).map(function (uid) {
                _this.players[uid].handCards = pukeData_1.sortCards(shuffled_cards.slice(cnt * 17, (cnt + 1) * 17));
                cnt++;
            });
            this.timeto = Object.keys(this.players)[Math.floor(Math.random() * 3)];
            this.dzCard = shuffled_cards.slice(51, 54);
            return new Promise(function (res, rej) {
                res({ event: "handOutCards", roomInfo: _this });
            });
        }
        return new Promise(function (res, rej) {
            res({ event: "preparePlayer", roomInfo: _this });
        });
    };
    roomInfo.prototype.nextPlayer = function () {
        var _this = this;
        var playerKeys = Object.keys(this.players);
        this.timeto = playerKeys[(playerKeys.indexOf(this.timeto) + 1) % 3];
        return new Promise(function (res, rej) {
            res({ event: "nextPlayer", roomInfo: _this });
        });
    };
    roomInfo.prototype.grabCard = function (data) {
        var _this = this;
        var uid = data.uid;
        this.players[uid].grabRes = data.param.grabRes;
        var grabCnt = 0;
        Object.keys(this.players).map(function (uid) {
            if (_this.players[uid].grabRes != -1)
                grabCnt++;
        });
        this.nextPlayer();
        if (grabCnt == 3) {
            while (this.players[this.timeto].grabRes == 0)
                this.nextPlayer();
            this.dzid = this.timeto;
            this.players[this.dzid].handCards = pukeData_1.sortCards(this.players[this.dzid].handCards.concat(this.dzCard));
            return new Promise(function (res, rej) {
                res({ event: "grabCardRes", roomInfo: _this });
            });
        }
        return new Promise(function (res, rej) {
            res({ event: "grabCard", roomInfo: _this });
        });
    };
    roomInfo.prototype.playOut = function (data) {
        var _this = this;
        var uid = data.uid;
        var param = data.param;
        if (param.outStyle) {
            var idList = param.outCards.map(function (item) { return item.id; });
            _.remove(this.players[uid].handCards, function (item) { return idList.indexOf(item['id']) >= 0; });
            this.lastOut = {
                outStyle: param.outStyle,
                uid: uid
            };
        }
        this.players[uid].outCards = param.outCards;
        this.nextPlayer();
        if (this.lastOut && this.dzid == this.lastOut.uid) {
            this.lastOut = null;
        }
        if (this.players[uid].handCards.length == 0) {
            return new Promise(function (res, rej) {
                res({ event: "gameOver", roomInfo: _this });
            });
        }
        return new Promise(function (res, rej) {
            res({ event: "playOut", roomInfo: _this });
        });
    };
    roomInfo.prototype.quit = function (data) {
        var _this = this;
        var uid = data.uid;
        delete this.players[uid];
        return new Promise(function (res, rej) {
            res({ event: "quit", roomInfo: _this });
        });
    };
    return roomInfo;
}());
exports.roomInfo = roomInfo;

cc._RF.pop();